﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DnDDB
{
    public partial class Main : Form
    {
        getMySQL get = new getMySQL();

        public Main()
        {
            InitializeComponent();
            updateListBoxAccount();
            //updateListBoxCharacter();
            updateComboBoxAlignment();
            updateComboBoxRace();
            //updateComboBoxClass();
            updateListBoxSkills();
            updateListBoxFeats();
            updateListBoxItemsAvailable();
            updateListBoxSpellsAvailable();
            updateListBoxLanguagesAvailable();
            startingPermissions();
            listBoxCharacter.SelectedIndexChanged += new EventHandler(listBoxCharacter_SelectedIndexChanged);
        }

        private void buttonCharacterAdd_Click(object sender, EventArgs e)
        {
            string name = Microsoft.VisualBasic.Interaction.InputBox("Enter a name for this character.", "Add Character", "", this.Location.X, this.Location.Y);
            if (name != "" && name != null)
            {
                foreach (object item in listBoxCharacter.Items)
                {
                    string itemstring = item.ToString();
                    if (itemstring == name)
                    {
                        MessageBox.Show("This character already exists!");
                        return;
                    }
                }
                Account acc = new Account(listBoxAccount.SelectedItem.ToString());
                Character newchar = new Character(name);
                acc.addCharacter(name);
                newchar.save();
                updateListBoxCharacter(acc);
            }
            listBoxCharacter.SelectedItem = name;
        }

        private void updateListBoxCharacter(Account acc)
        {
            listBoxCharacter.Items.Clear();
            foreach (string item in acc.getCharacter())
            {
                listBoxCharacter.Items.Add(item);
            }
        }

        private void updateComboBoxAlignment()
        {
            string dbcommand = "Select alignment from alignment;";
            DataTable dt = get.GetMySQLFilledTable(dbcommand);
            foreach (DataRow row in dt.Rows)
            {
                comboBoxAlignment.Items.Add(row["alignment"].ToString());
            }
        }

        private void updateComboBoxRace()
        {
            string dbcommand = "Select name from race;";
            DataTable dt = get.GetMySQLFilledTable(dbcommand);
            foreach (DataRow row in dt.Rows)
            {
                comboBoxRace.Items.Add(row["name"].ToString());
            }
        }

        private void updateComboBoxClass()
        {
            string dbcommand = "Select unique name from class;";
            DataTable dt = get.GetMySQLFilledTable(dbcommand);
            foreach (DataRow row in dt.Rows)
            {
                comboBoxRace.Items.Add(row["name"].ToString());
            }
        }
        private void buttonCharacterDelete_Click(object sender, EventArgs e)
        {
            /*
            string name = listBoxCharacter.SelectedItem.ToString();
            Account acc = new Account(listBoxCharacter.SelectedItem.ToString());

            updateListBoxCharacter(acc);
            clearAll();
            */
        }

        private void listBoxCharacter_SelectedIndexChanged(object sender, EventArgs e)
        {
            listBoxSkills.SelectedIndex = -1;
            if (listBoxCharacter.SelectedItems.Count == 1)
            {
                clearAll();
                Character dndchar = new Character(listBoxCharacter.SelectedItem.ToString());
                dndchar.load();
                textBoxName.Text = dndchar.getName();
                textBoxStrBase.Text = dndchar.getStr().ToString();
                textBoxDexBase.Text = dndchar.getDex().ToString();
                textBoxConBase.Text = dndchar.getCon().ToString();
                textBoxIntBase.Text = dndchar.getIntelligence().ToString();
                textBoxWisBase.Text = dndchar.getWis().ToString();
                textBoxChaBase.Text = dndchar.getCha().ToString();
                textBoxFundsinCopper.Text = dndchar.getCopper().ToString();
                textBoxCurrentHP.Text = dndchar.getCur_hp().ToString();
                textBoxMaxHP.Text = dndchar.getMax_hp().ToString();
                comboBoxRace.SelectedItem = dndchar.getRace();
                comboBoxAlignment.SelectedItem = dndchar.getAlignment();
                updateListBoxPartyCharactersAvailable(dndchar.getName());
                updateListBoxpartyCharactersInParty(dndchar.getName());
                updateAllEquipment(dndchar);
                updateListBoxSpellsKnown(dndchar);
                updateListBoxLanguagesKnown(dndchar);
                updateListBoxFeatsKnown(dndchar);
            }
            else
            {
                clearAll();
            }
        }

        private void clearAll()
        {
            textBoxName.Text = "";
            textBoxStrBase.Text = "";
            textBoxStrMod.Text = "";
            textBoxDexBase.Text = "";
            textBoxConBase.Text = "";
            textBoxConMod.Text = "";
            textBoxIntBase.Text = "";
            textBoxIntMod.Text = "";
            textBoxWisBase.Text = "";
            textBoxWisMod.Text = "";
            textBoxChaBase.Text = "";
            textBoxChaMod.Text = "";
            textBoxCurrentHP.Text = "";
            textBoxMaxHP.Text = "";
            textBoxFundsinCopper.Text = "";
            listBoxSkills.SelectedIndex = -1;
            listBoxFeatsAvailable.SelectedIndex = -1;
            listBoxItemsAvailable.SelectedIndex = -1;
            listBoxSpellsKnown.Items.Clear();
            listBoxFeatsKnown.Items.Clear();
            listBoxItemsOwned.Items.Clear();
            listBoxLanguagesKnown.Items.Clear();
            listBoxEquipmentArmorEquipped.Items.Clear();
            listBoxEquipmentWeaponsEquipped.Items.Clear();
            listBoxEquipmentWeaponsAvailable.Items.Clear();
            listBoxEquipmentArmorAvailable.Items.Clear();
            listBoxPartyCharactersAvailable.Items.Clear();
            listBoxPartyCharactersInParty.Items.Clear();
            listBoxItemsOwned.Items.Clear();
        }

        private void updateListBoxSkills()
        {
            string dbcommand = "Select name from skill;";
            DataTable dt = get.GetMySQLFilledTable(dbcommand);
            foreach (DataRow row in dt.Rows)
            {
                listBoxSkills.Items.Add(row["name"].ToString());
            }
        }

        private void buttonReroll_Click(object sender, EventArgs e)
        {
            textBoxStrBase.Text = Mechanic.statroll().ToString();
            textBoxConBase.Text = Mechanic.statroll().ToString();
            textBoxDexBase.Text = Mechanic.statroll().ToString();
            textBoxIntBase.Text = Mechanic.statroll().ToString();
            textBoxWisBase.Text = Mechanic.statroll().ToString();
            textBoxChaBase.Text = Mechanic.statroll().ToString();
        }

        private void saveToDatabaseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            characterSave();
        }

        private void listBoxSkills_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBoxSkills.SelectedItems.Count == 1 && listBoxCharacter.SelectedItems.Count == 1)
            {
                Skill skill = new Skill(listBoxSkills.SelectedItem.ToString());
                skill.findranks(listBoxCharacter.SelectedItem.ToString());
                textBoxSkillRanks.Text = skill.getRanks().ToString();
                textBoxSkillBonus.Text = "0";
                textBoxSkillTotal.Text = skill.getRanks().ToString();
            }
            else
            {
                textBoxSkillRanks.Text = "";
                textBoxSkillBonus.Text = "";
                textBoxSkillTotal.Text = "";
            }
        }

        private void buttonCharacterSkillsSave_Click(object sender, EventArgs e)
        {
            if (listBoxSkills.SelectedItems.Count == 1 && listBoxCharacter.SelectedItems.Count == 1)
            {
                Skill skill = new Skill(listBoxSkills.SelectedItem.ToString());
                skill.setCharacterName(listBoxCharacter.SelectedItem.ToString());
                skill.setRanks(Convert.ToInt32(textBoxSkillRanks.Text));
                skill.save();
            }
        }

        private void textBoxSkillRanks_TextChanged(object sender, EventArgs e)
        {
            updateSkillTotal();
        }

        private void updateSkillTotal()
        {
            if (textBoxSkillBonus.Text != "" && textBoxSkillRanks.Text != "")
            {
                textBoxSkillTotal.Text = (Convert.ToInt32(textBoxSkillRanks.Text) + Convert.ToInt32(textBoxSkillBonus.Text)).ToString();
            }
        }

        private void textBoxSkillBonus_TextChanged(object sender, EventArgs e)
        {
            updateSkillTotal();
        }

        /// <summary>
        /// Fills the Feat listbox with all known feats. Should only be called when the program initializes.
        /// </summary>
        private void updateListBoxFeats()
        {
            string dbcommand = "Select name from feat;";
            DataTable dt = get.GetMySQLFilledTable(dbcommand);
            foreach (DataRow row in dt.Rows)
            {
                listBoxFeatsAvailable.Items.Add(row["name"].ToString());
            }
        }

        /// <summary>
        /// Fills the ItemsAvailable listbox with all known items. Should only be called when the program initializes.
        /// </summary>
        private void updateListBoxItemsAvailable()
        {
            string dbcommand = "Select name from equipment;";
            DataTable dt = get.GetMySQLFilledTable(dbcommand);
            foreach (DataRow row in dt.Rows)
            {
                listBoxItemsAvailable.Items.Add(row["name"].ToString());
            }
        }

        private void buttonInventoryAddItem_Click(object sender, EventArgs e)
        {
            if (listBoxItemsAvailable.SelectedIndex > -1)
            {
                foreach (object item in listBoxItemsOwned.Items)
                {
                    if (item.ToString() == listBoxItemsAvailable.SelectedItem.ToString())
                    {
                        listBoxItemsOwned.SelectedIndex = listBoxItemsOwned.Items.IndexOf(item);
                        return;
                    }
                }
                listBoxItemsOwned.Items.Add(listBoxItemsAvailable.SelectedItem.ToString());
                Equipment equip = new Equipment(listBoxItemsAvailable.SelectedItem.ToString());
                if (equip.weaponp())
                    listBoxEquipmentWeaponsAvailable.Items.Add(equip.getEquipmentName());
                else if (equip.armorp())
                    listBoxEquipmentArmorAvailable.Items.Add(equip.getEquipmentName());
            }
        }

        private void buttonInventoryRemoveItem_Click(object sender, EventArgs e)
        {
            if (listBoxItemsOwned.SelectedIndex > -1)
            {
                Equipment equip = new Equipment(listBoxItemsOwned.SelectedItem.ToString());
                listBoxItemsOwned.Items.RemoveAt(listBoxItemsOwned.SelectedIndex);
                if (equip.weaponp())
                {
                    listBoxEquipmentWeaponsAvailable.Items.Remove(equip.getEquipmentName());
                    if (listBoxEquipmentWeaponsEquipped.Items.Contains(equip.getEquipmentName()))
                        listBoxEquipmentWeaponsEquipped.Items.Remove(equip.getEquipmentName());
                }
                else if (equip.armorp())
                {
                    listBoxEquipmentArmorAvailable.Items.Remove(equip.getEquipmentName());
                    if (listBoxEquipmentArmorEquipped.Items.Contains(equip.getEquipmentName()))
                        listBoxEquipmentArmorEquipped.Items.Remove(equip.getEquipmentName());
                }
                resetInventoryTextboxes();
            }
        }

        private void listBoxItemsAvailable_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBoxItemsAvailable.SelectedIndex > -1)
            {
                string dbcommand = "Select * from Equipment where name='" + listBoxItemsAvailable.SelectedItem.ToString() + "';";
                DataRow dt = get.GetMySQLFilledRow(dbcommand);
                textBoxItemName.Text = dt["name"].ToString();
                textBoxItemValueInCoppers.Text = dt["value"].ToString();
                textBoxItemWeight.Text = dt["weight"].ToString();
                numericUpDownItemNumberOwned.Value = 0;
            }
        }

        private void updateListBoxAccount()
        {
            string dbcommand = "Select logon from account;";
            DataTable dt = get.GetMySQLFilledTable(dbcommand);
            foreach (DataRow row in dt.Rows)
            {
                listBoxAccount.Items.Add(row["logon"].ToString());
            }
        }

        private void listBoxAccount_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBoxAccount.SelectedIndex > -1)
            {
                listBoxCharacter.Enabled = true;
                buttonCharacterAdd.Enabled = true;
                buttonAccountEdit.Enabled = true;
                Account acc = new Account(listBoxAccount.SelectedItem.ToString());
                checkBoxAccountPaid.Checked = acc.getPaidp();
                textBoxAccountPassword.Text = acc.getPassword();
                textBoxAccountEmail.Text = acc.getEmail();
                updateListBoxCharacter(acc);
            }
            else
            {
                startingPermissions();
            }
        }

        private void startingPermissions()
        {
            listBoxCharacter.Enabled = false;
            buttonCharacterAdd.Enabled = false;
            buttonAccountEdit.Enabled = false;
            checkBoxAccountPaid.Checked = false;
            textBoxAccountPassword.Text = "";
            textBoxAccountEmail.Text = "";
        }

        private void buttonCharacterSave_Click(object sender, EventArgs e)
        {
            characterSave();
        }

        private void characterSave()
        {
            Character dndchar = new Character(listBoxCharacter.SelectedItem.ToString());
            dndchar.setStr(Convert.ToInt32(textBoxStrBase.Text));
            dndchar.setDex(Convert.ToInt32(textBoxDexBase.Text));
            dndchar.setCon(Convert.ToInt32(textBoxConBase.Text));
            dndchar.setIntelligence(Convert.ToInt32(textBoxIntBase.Text));
            dndchar.setWis(Convert.ToInt32(textBoxWisBase.Text));
            dndchar.setCha(Convert.ToInt32(textBoxChaBase.Text));
            dndchar.setCopper(Convert.ToInt32(textBoxFundsinCopper.Text));
            dndchar.setMax_hp(Convert.ToInt32(textBoxMaxHP.Text));
            dndchar.setCur_hp(Convert.ToInt32(textBoxCurrentHP.Text));
            dndchar.setRace(comboBoxRace.SelectedItem.ToString());
            dndchar.setAlignment(comboBoxAlignment.SelectedItem.ToString());
            int skillindex = listBoxSkills.SelectedIndex;
            foreach (string armor in listBoxEquipmentArmorAvailable.Items)
            {
                Armor thearmor = new Armor(armor);
                dndchar.addArmor(thearmor);
            }
            foreach (string armor in listBoxEquipmentArmorEquipped.Items)
            {
                Armor thearmor = new Armor(armor, true);
                dndchar.addArmor(thearmor);
            }
            foreach (string weapon in listBoxEquipmentWeaponsAvailable.Items)
            {
                Weapon theweapon = new Weapon(weapon);
                dndchar.addWeapon(theweapon);
            }
            foreach (string weapon in listBoxEquipmentWeaponsEquipped.Items)
            {
                Weapon theweapon = new Weapon(weapon, true);
                dndchar.addWeapon(theweapon);
            }
            foreach (string item in listBoxItemsOwned.Items)
            {
                Equipment equip = new Equipment(item);
                dndchar.addEquipment(equip);
            }
            foreach (string spell in listBoxSpellsKnown.Items)
            {
                Spell knownspell = new Spell(spell);
                dndchar.addKnownSpell(knownspell);
            }
            foreach (string language in listBoxLanguagesKnown.Items)
            {
                dndchar.addLanguage(language);
            }
            foreach (string feat in listBoxFeatsKnown.Items)
            {
                dndchar.addFeat(feat);
            }
            string charname = textBoxName.Text;
            if (dndchar.save(charname))
            {
                Account acc = new Account(listBoxAccount.SelectedItem.ToString());
                updateListBoxCharacter(acc);
                listBoxCharacter.SelectedItem = charname;
                listBoxSkills.SelectedIndex = skillindex;
            }
        }

        private void listBoxEquipmentArmorAvailable_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBoxEquipmentArmorAvailable.SelectedIndex > -1)
            {
                listBoxEquipmentArmorEquipped.SelectedIndex = -1;
                setArmorTextboxes(new Armor(listBoxEquipmentArmorAvailable.SelectedItem.ToString()));
            }
            else if (listBoxEquipmentArmorAvailable.SelectedIndex <= -1 && listBoxEquipmentArmorEquipped.SelectedIndex <= -1)
            {
                resetArmorTextboxes();
            }
        }

        private void setArmorTextboxes(Armor armor)
        {
            textBoxEquipmentArmorName.Text = armor.getEquipmentName();
            textBoxEquipmentBaseAC.Text = armor.getACbonus().ToString();
            textBoxEquipmentArmorEnchant.Text = armor.getEnchantment().ToString();
            textBoxEquipmentMaxAC.Text = (armor.getACbonus() + armor.getEnchantment()).ToString();
            textBoxEquipmentCheckPenalty.Text = armor.getArmorCheckPenalty().ToString();
            textBoxEquipmentArmorValueinCoppers.Text = armor.getCost().ToString();
            textBoxEquipmentMaxSpeed.Text = armor.getSpeed().ToString();
        }

        private void listBoxEquipmentArmorEquipped_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBoxEquipmentArmorEquipped.SelectedIndex > -1)
            {
                listBoxEquipmentArmorAvailable.SelectedIndex = -1;
                setArmorTextboxes(new Armor(listBoxEquipmentArmorEquipped.SelectedItem.ToString()));
            }
            else if (listBoxEquipmentArmorAvailable.SelectedIndex <= -1 && listBoxEquipmentArmorEquipped.SelectedIndex <= -1)
            {
                resetArmorTextboxes();
            }
        }

        private void resetArmorTextboxes()
        {
            textBoxEquipmentArmorName.Text = "";
            textBoxEquipmentBaseAC.Text = "";
            textBoxEquipmentArmorEnchant.Text = "";
            textBoxEquipmentMaxAC.Text = "";
            textBoxEquipmentCheckPenalty.Text = "";
            textBoxEquipmentArmorValueinCoppers.Text = "";
            textBoxEquipmentMaxSpeed.Text = "";
        }

        private void buttonEquipmentArmorEquip_Click(object sender, EventArgs e)
        {
            if (listBoxEquipmentArmorAvailable.SelectedIndex > -1)
            {
                int index = listBoxEquipmentArmorEquipped.Items.Add(listBoxEquipmentArmorAvailable.SelectedItem.ToString());
                listBoxEquipmentArmorAvailable.Items.RemoveAt(listBoxEquipmentArmorAvailable.SelectedIndex);
                listBoxEquipmentArmorEquipped.SelectedIndex = index;
            }
        }

        private void buttonEquipmentArmorUnequip_Click(object sender, EventArgs e)
        {
            if (listBoxEquipmentArmorEquipped.SelectedIndex > -1)
            {
                int index = listBoxEquipmentArmorAvailable.Items.Add(listBoxEquipmentArmorEquipped.SelectedItem.ToString());
                listBoxEquipmentArmorEquipped.Items.RemoveAt(listBoxEquipmentArmorEquipped.SelectedIndex);
                listBoxEquipmentArmorAvailable.SelectedIndex = index;
            }
        }

        private void listBoxEquipmentWeaponsAvailable_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBoxEquipmentWeaponsAvailable.SelectedIndex > -1)
            {
                listBoxEquipmentWeaponsEquipped.SelectedIndex = -1;
                setWeaponTextboxes(new Weapon(listBoxEquipmentWeaponsAvailable.SelectedItem.ToString()));
            }
            else if (listBoxEquipmentWeaponsAvailable.SelectedIndex <= -1 && listBoxEquipmentWeaponsEquipped.SelectedIndex <= -1)
            {
                resetWeaponTextboxes();
            }
        }

        private void setWeaponTextboxes(Weapon weapon)
        {
            textBoxEquipmentWeaponName.Text = weapon.getEquipmentName();
            textBoxEquipmentWeaponType.Text = weapon.getType();
            textBoxEquipmentWeaponDamage.Text = (string)weapon.getDamage(false);
            textBoxEquipmentCriticalMinimum.Text = weapon.getCritMinimum().ToString();
            textBoxEquipmentCriticalMaximum.Text = "20";
            textBoxEquipmentCriticalMultiplier.Text = weapon.getCritMultiplier().ToString();
        }

        private void resetWeaponTextboxes()
        {
            textBoxEquipmentWeaponName.Text = "";
            textBoxEquipmentWeaponType.Text = "";
            textBoxEquipmentWeaponDamage.Text = "";
            textBoxEquipmentCriticalMinimum.Text = "";
            textBoxEquipmentCriticalMaximum.Text = "";
            textBoxEquipmentCriticalMultiplier.Text = "";
        }

        private void listBoxEquipmentWeaponsEquipped_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBoxEquipmentWeaponsEquipped.SelectedIndex > -1)
            {
                listBoxEquipmentWeaponsAvailable.SelectedIndex = -1;
                setWeaponTextboxes(new Weapon(listBoxEquipmentWeaponsEquipped.SelectedItem.ToString()));
            }
            else if (listBoxEquipmentArmorAvailable.SelectedIndex <= -1 && listBoxEquipmentArmorEquipped.SelectedIndex <= -1)
            {
                resetWeaponTextboxes();
            }
        }

        private void listBoxItemsOwned_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBoxItemsOwned.SelectedIndex > -1)
            {
                Equipment equip = new Equipment(listBoxItemsOwned.SelectedItem.ToString());
                setInventoryTextBoxes(equip);
            }
        }

        private void setInventoryTextBoxes(Equipment equip)
        {
            textBoxItemName.Text = equip.getEquipmentName();
            textBoxItemValueInCoppers.Text = equip.getCost().ToString();
            textBoxItemWeight.Text = equip.getWeight().ToString();
            numericUpDownItemNumberOwned.Value = 0;
        }

        private void resetInventoryTextboxes()
        {
            textBoxItemName.Text = "";
            textBoxItemValueInCoppers.Text = "";
            textBoxItemWeight.Text = "";
            numericUpDownItemNumberOwned.Value = 0;
        }

        private void listBoxSpellsAvailable_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBoxSpellsAvailable.SelectedIndex > -1)
            {
                /*
                foreach (string spell in listBoxSpellsKnown.Items)
                {
                    if (spell == listBoxSpellsAvailable.SelectedItem.ToString())
                    {
                        listBoxSpellsKnown.SelectedIndex = listBoxSpellsKnown.Items.IndexOf(spell);
                        return;
                    }
                }
                int index = listBoxEquipmentArmorAvailable.Items.Add(listBoxEquipmentArmorEquipped.SelectedItem.ToString());
                listBoxEquipmentArmorEquipped.Items.RemoveAt(listBoxEquipmentArmorEquipped.SelectedIndex);
                listBoxEquipmentArmorAvailable.SelectedIndex = index;
                */
            }
        }

        private void listBoxSpellsKnown_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void listBoxLanguagesAvailable_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void listBoxLanguagesKnown_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void updateListBoxSpellsAvailable()
        {
            string dbcommand = "Select name from spell;";
            DataTable dt = get.GetMySQLFilledTable(dbcommand);
            foreach (DataRow row in dt.Rows)
            {
                listBoxSpellsAvailable.Items.Add(row["name"].ToString());
            }
        }

        private void updateListBoxLanguagesAvailable()
        {
            string dbcommand = "Select name from language;";
            DataTable dt = get.GetMySQLFilledTable(dbcommand);
            foreach (DataRow row in dt.Rows)
            {
                listBoxLanguagesAvailable.Items.Add(row["name"].ToString());
            }
        }

        private void buttonFeatTake_Click(object sender, EventArgs e)
        {
            if (listBoxFeatsAvailable.SelectedIndex > -1)
            {
                Feat feat = new Feat(listBoxFeatsAvailable.SelectedItem.ToString());
                //if (!feat.findStackable())
                //{
                foreach (string item in listBoxFeatsKnown.Items)
                {
                    if (item == listBoxFeatsAvailable.SelectedItem.ToString())
                    {
                        MessageBox.Show("You cannot select this feat multiple times.");
                        return;
                    }
                }
                //}
                //else
                //{
                listBoxFeatsKnown.Items.Add(listBoxFeatsAvailable.SelectedItem.ToString());
                //}
            }

        }

        private void buttonFeatRemove_Click(object sender, EventArgs e)
        {
            if (listBoxFeatsKnown.SelectedIndex > -1)
            {
                listBoxFeatsKnown.Items.RemoveAt(listBoxFeatsKnown.SelectedIndex);
            }
        }

        private void buttonSpellsAddSpell_Click(object sender, EventArgs e)
        {
            if (listBoxSpellsAvailable.SelectedIndex > -1)
            {
                foreach (string spell in listBoxSpellsKnown.Items)
                {
                    if (spell == listBoxSpellsAvailable.SelectedItem.ToString())
                    {
                        listBoxSpellsKnown.SelectedIndex = listBoxSpellsKnown.Items.IndexOf(spell);
                        return;
                    }
                }
                listBoxSpellsKnown.Items.Add(listBoxSpellsAvailable.SelectedItem.ToString());
            }
        }

        private void buttonSpellsRemoveSpell_Click(object sender, EventArgs e)
        {
            if (listBoxSpellsKnown.SelectedIndex > -1)
            {
                listBoxSpellsKnown.Items.RemoveAt(listBoxSpellsKnown.SelectedIndex);
            }
        }

        private void buttonLanguagesAdd_Click(object sender, EventArgs e)
        {
            if (listBoxLanguagesAvailable.SelectedIndex > -1)
            {
                foreach (string language in listBoxLanguagesKnown.Items)
                {
                    if (language == listBoxLanguagesAvailable.SelectedItem.ToString())
                    {
                        listBoxLanguagesKnown.SelectedIndex = listBoxLanguagesKnown.Items.IndexOf(language);
                        return;
                    }
                }
                listBoxLanguagesKnown.Items.Add(listBoxLanguagesAvailable.SelectedItem.ToString());
            }
        }

        private void buttonLanguagesRemove_Click(object sender, EventArgs e)
        {
            if (listBoxLanguagesAvailable.SelectedIndex > -1)
            {
                listBoxLanguagesKnown.Items.RemoveAt(listBoxLanguagesKnown.SelectedIndex);
            }
        }

        private void buttonAccountEdit_Click(object sender, EventArgs e)
        {
            Account acc = new Account(listBoxAccount.SelectedItem.ToString());
            acc.setPaid(checkBoxAccountPaid.Checked);
            acc.setPassword(textBoxAccountPassword.Text);
            acc.setEmail(textBoxAccountEmail.Text);
            acc.save();
        }

        private void buttonPartyAdd_Click(object sender, EventArgs e)
        {
            if (listBoxPartyCharactersAvailable.SelectedIndex > -1 && listBoxCharacter.SelectedIndex > -1)
            {
                foreach (string item in listBoxPartyCharactersInParty.Items)
                {
                    if (item == listBoxPartyCharactersAvailable.SelectedItem.ToString())
                    {
                        listBoxPartyCharactersInParty.SelectedIndex = listBoxPartyCharactersInParty.Items.IndexOf(item);
                        return;
                    }
                }
                if (Party.party_characters(listBoxCharacter.SelectedItem.ToString(), listBoxPartyCharactersAvailable.SelectedItem.ToString()))
                    listBoxPartyCharactersInParty.Items.Add(listBoxPartyCharactersAvailable.SelectedItem.ToString());
            }
        }

        private void buttonPartyRemove_Click(object sender, EventArgs e)
        {
            if (listBoxCharacter.SelectedIndex > -1)
            {
                if (Party.unparty(listBoxCharacter.SelectedItem.ToString()))
                    listBoxPartyCharactersInParty.Items.Clear();
            }
        }

        private void buttonAccountAdd_Click(object sender, EventArgs e)
        {
            string name = Microsoft.VisualBasic.Interaction.InputBox("Enter a name for this account.", "Add account", "", this.Location.X, this.Location.Y);
            if (name != "" && name != null)
            {
                foreach (object item in listBoxAccount.Items)
                {
                    string itemstring = item.ToString();
                    if (itemstring == name)
                    {
                        MessageBox.Show("This account already exists!");
                        return;
                    }
                }
                Account.addAccount(name, "", false, "");
                listBoxAccount.Items.Add(name);
            }
        }

        private void buttonEquipmentWeaponEquip_Click(object sender, EventArgs e)
        {
            if (listBoxEquipmentWeaponsAvailable.SelectedIndex > -1)
            {
                int index = listBoxEquipmentWeaponsEquipped.Items.Add(listBoxEquipmentWeaponsAvailable.SelectedItem.ToString());
                listBoxEquipmentWeaponsAvailable.Items.RemoveAt(listBoxEquipmentWeaponsAvailable.SelectedIndex);
                listBoxEquipmentWeaponsEquipped.SelectedIndex = index;
            }
        }

        private void buttonEquipmentWeaponUnequip_Click(object sender, EventArgs e)
        {
            if (listBoxEquipmentWeaponsEquipped.SelectedIndex > -1)
            {
                int index = listBoxEquipmentWeaponsAvailable.Items.Add(listBoxEquipmentWeaponsEquipped.SelectedItem.ToString());
                listBoxEquipmentWeaponsEquipped.Items.RemoveAt(listBoxEquipmentWeaponsEquipped.SelectedIndex);
                listBoxEquipmentWeaponsAvailable.SelectedIndex = index;
            }
        }

        private void updateListBoxPartyCharactersAvailable(string charactername)
        {
            listBoxPartyCharactersAvailable.Items.Clear();
            List<string> members = Character.getPossiblePartyMembers(charactername);
            foreach (string member in members)
            {
                listBoxPartyCharactersAvailable.Items.Add(member);
            }
        }

        private void updateListBoxpartyCharactersInParty(string charactername)
        {
            listBoxPartyCharactersInParty.Items.Clear();
            List<string> members = Party.party_members(charactername);
            foreach (string member in members)
            {
                listBoxPartyCharactersInParty.Items.Add(member);
            }
        }

        private void updateAllEquipment(Character dndchar)
        {
            foreach (Armor armor in dndchar.getArmor())
            {
                if (armor.getEquipped())
                {
                    listBoxEquipmentArmorEquipped.Items.Add(armor.getEquipmentName());
                }
                else
                {
                    listBoxEquipmentArmorAvailable.Items.Add(armor.getEquipmentName());
                }
            }
            foreach (Weapon weapon in dndchar.getWeapons())
            {
                if (weapon.getEquipped())
                {
                    listBoxEquipmentWeaponsEquipped.Items.Add(weapon.getEquipmentName());
                }
                else
                {
                    listBoxEquipmentWeaponsAvailable.Items.Add(weapon.getEquipmentName());
                }
            }
            foreach (Equipment equipment in dndchar.getEquipment())
            {
                if (!listBoxItemsOwned.Items.Contains(equipment.getEquipmentName()))
                    listBoxItemsOwned.Items.Add(equipment.getEquipmentName());
            }
        }

        private void updateListBoxSpellsKnown(Character dndchar)
        {
            foreach (Spell spell in dndchar.getKnownSpells())
                listBoxSpellsKnown.Items.Add(spell.getName());
        }
        private void updateListBoxLanguagesKnown(Character dndchar)
        {
            foreach (string language in dndchar.getLanguages())
                listBoxLanguagesKnown.Items.Add(language);
        }

        private void updateListBoxFeatsKnown(Character dndchar)
        {
            foreach (string feat in dndchar.getFeats())
                listBoxFeatsKnown.Items.Add(feat);
        }
    }
}
